/**
 * Copyright (C) 2008 ChangSha WangWin Science & Technology CO,.LTD. All rights reserved.
 * WangWin PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Copyright (C) 2002-2008 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * This software is the confidential and proprietary information of
 * WuZeven, Personal. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with WuZeven.
 */

package com.kingcore.pda.database.bean.impl;

import java.util.HashMap;

import org.apache.log4j.Logger;

import com.kingcore.pda.database.bean.DatabaseDictionary;

/**
 * <p>SqlServer 数据字典对象。</p>
 * @author Zeven on 2008-11-25
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @see		Object#hashCode()
 * @see		HashMap
 * @since	JDK5
 */

public class SqlServerDatabaseDictionary implements DatabaseDictionary {

	private final static Logger log = Logger.getLogger(SqlServerDatabaseDictionary.class);
	
	/**
	 * <p>java方法的说明...</p>
	 * @author Zeven on 2008-11-25
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public String createSqlOfTableInforByCondition(String tableCondition) {
		StringBuffer sbf=new StringBuffer();
		//sbf.delete(0, sbf.length());
		sbf.append(" Select t.Id As Tblid, t.Name As Table_Name,")
			.append(" 'TABLE' As Table_Type, T2.Value As Comments, t.Xtype")
			.append(" From Sysobjects t")
			.append(" Left Outer Join Sysproperties T2 On t.Id = T2.Id")
			.append(" And T2.Type = 3 And T2.Name = 'MS_Description'")
			.append(" Where ("+tableCondition+") and ((t.Xtype = 'U' Or t.Xtype = 'S'))")
			.append(" Order By Table_Name");
		log.debug(sbf);
		return sbf.toString();
	}

	public String createSqlOfTableInforByName(String dbName, String tableName ) {
		StringBuffer sbf=new StringBuffer();
		//sbf.delete(0, sbf.length());
		sbf.append(" Select t.Id As Tblid, t.Name As Table_Name,")
			.append(" 'TABLE' As Table_Type, T2.Value As Comments, t.Xtype")
			.append(" From Sysobjects t")
			.append(" Left Outer Join Sysproperties T2 On t.Id = T2.Id")
			.append(" And T2.Type = 3 And T2.Name = 'MS_Description'")
			.append(" Where ((t.Xtype = 'U' Or t.Xtype = 'S')")
			.append(" And Upper(t.Name) = '"+tableName.toUpperCase()+"')")
			.append(" Order By Table_Name");
		log.debug(sbf);
		return sbf.toString();
	}

	public String createSqlOfColumnInforByCondition(String dbName, String tableId, String tableEn) {
		StringBuffer sbf=new StringBuffer();
		//sbf.delete(0, sbf.length());
		sbf.append(" Select t.colstat As Isautoincrement, t.Name Column_Name,")   //-1 As Isautoincrement
			.append(" T3.Name + (Case T3.Name")
			.append(" When 'varchar' Then '(' + Convert(Varchar, t.Length) + ')'")
			.append(" When 'char' Then '(' + Convert(Varchar, t.Length) + ')'")
			.append(" When 'numeric' Then '(' + Convert(Varchar, t.Xprec) +")
			.append(" (Case t.Xscale")
			.append(" When 0 Then ''")
			.append(" Else ',' + Convert(Varchar, t.Xscale)")
			.append(" End) + ')'")
			.append(" Else ''")
			.append(" End) Typefull,")
			.append(" Case t.Isnullable")
			.append(" When '0' Then 'N'")
			.append(" When '1' Then 'Y'")
			.append(" End Nullable, T2.Value Comments, t.Colid, t.*")
			.append(" From Syscolumns t")
			.append(" Left Outer Join Sysproperties T2")
			.append(" On (t.Id = T2.Id And t.Colid = T2.Smallid")
			.append(" And T2.Type = 4 And T2.Name = 'MS_Description'), Systypes T3")
			.append(" Where (t.Id = '"+tableId+"')")
			.append(" And t.Xusertype = T3.Xusertype")
			.append(" Order By Colorder Asc");
		log.debug(sbf);
		return sbf.toString();
	}

	public String createSqlOfConstraintColumns(String dbName, String is_tblname, String constraint_name) {
		return null;
	}

	public String createSqlOfConstraints(String dbName, String tableEn) {
		return null;
	}

	public String createUpdateCommentSql(String dbName, String tableEn, String colEn, String comment) {
		// TODO Auto-generated method stub
		return null;
	}

}
